package com.ies.dx.dex.file;

import java.util.Iterator;
import java.util.Vector;

import com.ies.dx.util.Input;

public class dex_file
{
    private header_item header;
    private Vector string_ids = new Vector(); //string_id_item
    private Vector type_ids = new Vector(); // type_id_item
    private Vector proto_ids = new Vector(); // proto_id_item
    private Vector field_ids = new Vector(); // field_id_item
    private Vector method_ids = new Vector(); // method_id_item
    private Vector class_defs = new Vector(); // class_def_item
    private byte[] data; 
    private byte[] link_data;
    
    
    public void readData(Input dis)
    {
        /*read string data*/
        for (Iterator it = string_ids.iterator(); it.hasNext();)
        {
            string_id_item sii = (string_id_item)it.next();
            sii.readData(dis);
        }
        /*read proto data*/
        for (Iterator it = proto_ids.iterator(); it.hasNext();)
        {
            proto_id_item pii = (proto_id_item)it.next();
            pii.readData(dis);
        }        

    }
    
    
    public void read(Input dis)
    {
        int offset = 0;
        try
        {
            /*read header*/
            header = new header_item(this);
            header.read(dis);
            /*read string ids*/
            offset = dis.getCursor();
            dis.skipBytes(offset - header.getStringIdsOffset());
            int size = header.getStringIdsSize();
            for (int i = 0; i < size; i ++)
            {
                string_id_item sii = new string_id_item(this);
                sii.read(dis);
                string_ids.add(sii);
            }                 
            /*read type ids*/
            offset = dis.getCursor();
            dis.skipBytes(offset - header.getTypeIdsOffset());
            size = header.getTypeIdsSize();
            for (int i = 0; i < size; i ++)
            {
                type_id_item tii = new type_id_item(this);
                tii.read(dis);
                type_ids.add(tii);
            }                 
            
            /*read proto ids*/
            offset = dis.getCursor();
            dis.skipBytes(offset - header.getProtoIdsOff());
            size = header.getProtoIdsSize();
            for (int i = 0; i < size; i ++)
            {
                proto_id_item pii = new proto_id_item(this);
                pii.read(dis);                
                proto_ids.add(pii);                
            }                 
            
            /*read field ids*/
            offset = dis.getCursor();
            dis.skipBytes(offset - header.getFieldIdsOff());
            size = header.getFieldIdsSize();
            for (int i = 0; i < size; i ++)
            {
                field_id_item fii = new field_id_item(this);
                fii.read(dis);
                field_ids.add(fii);
            }                 
            
            /*read method ids*/
            offset = dis.getCursor();
            dis.skipBytes(offset - header.getMethodIdsOff());
            size = header.getMethodIdsSize();
            for (int i = 0; i < size; i ++)
            {
                method_id_item mii = new method_id_item(this);
                mii.read(dis);
                method_ids.add(mii);
            }                 
            
            /*read class defs*/
            offset = dis.getCursor();
            dis.skipBytes(offset - header.getClassDefsOff());
            size = header.getClassDefsSize();
            for (int i = 0; i < size; i ++)
            {
                class_def_item cdi = new class_def_item(this);
                cdi.read(dis);
                class_defs.add(cdi);
            }    
            
            offset = dis.getCursor();
            dis.skipBytes(offset - header.getDataOff());
            size = header.getDataSize(); 
            data = new byte[size];
            dis.read(data);
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }    
    
    public void show()
    {
        /*Header*/
        header.show();
        /*String ids*/
        System.out.println("------------------------------------------------------------------");
        for (Iterator it = string_ids.iterator(); it.hasNext();)
        {
            string_id_item sii = (string_id_item)it.next();
            sii.show();
        }
        System.out.println("------------------------------------------------------------------");
        
        System.out.println("------------------------------------------------------------------");
        for (Iterator it = type_ids.iterator(); it.hasNext();)
        {
            type_id_item tii = (type_id_item)it.next();
            tii.show();
        }
        System.out.println("------------------------------------------------------------------");

        System.out.println("------------------------------------------------------------------");
        for (Iterator it = proto_ids.iterator(); it.hasNext();)
        {
            proto_id_item pii = (proto_id_item)it.next();
            pii.show();
        }
        System.out.println("------------------------------------------------------------------");

        System.out.println("------------------------------------------------------------------");
        for (Iterator it = field_ids.iterator(); it.hasNext();)
        {
            field_id_item fii = (field_id_item)it.next();
            fii.show();
        }
        System.out.println("------------------------------------------------------------------");

        System.out.println("------------------------------------------------------------------");
        for (Iterator it = method_ids.iterator(); it.hasNext();)
        {
            method_id_item mii = (method_id_item)it.next();
            mii.show();
        }
        System.out.println("------------------------------------------------------------------");

        System.out.println("------------------------------------------------------------------");
        for (Iterator it = class_defs.iterator(); it.hasNext();)
        {
            class_def_item cdi = (class_def_item)it.next();
            cdi.show();
        }
        System.out.println("------------------------------------------------------------------");
        
    }
    
    public String getString(int id)
    {
        return ((string_id_item)string_ids.get(id)).getData();
    }
    
    public String getType(int id)
    {
        return ((type_id_item)type_ids.get(id)).getData();
    }
    
    public String getProtoType(int id)
    {
        return ((proto_id_item)proto_ids.get(id)).getData();
    }
}
